package ah.ddh.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownFlash {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		new DownFlash().ListFile();
	}
	
	public void downHtml() throws Exception {
		String fileName = "0.html";
		for (int i=1;i<=133;i++) {
			if (i<10) {
				fileName = "j2me00"+i+".html";
			} else if (i<100) {
				fileName = "j2me0"+i+".html";
			} else {
				fileName = "j2me"+i+".html";
			}
			
			URL url = new URL("http://ftp5-idc.pconline.com.cn/pub/download/ftpdown/pcsoft/pcedu/video/j2me/"+fileName);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			HttpURLConnection.setFollowRedirects(true);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); 
			
			InputStream in = conn.getInputStream();
			File file = new File("D:/j2me_download/"+fileName);
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream out = new FileOutputStream(file);
			
			int length = 0;
			byte[] b = new byte[1024000];
			while ((length=in.read(b))>0){
				out.write(b, 0, length);
			}
			in.close();
			out.close();
			conn.disconnect();
		}
	}
	
	public void ListFile() {
		File dir = new File("D:/j2me_download");
		File[] files = dir.listFiles();
		String prefix = "http://ftp5-idc.pconline.com.cn/pub/download/ftpdown/pcsoft/pcedu/video/j2me/";
		for (File file : files) {
			try {
				BufferedReader br = new BufferedReader(new FileReader(file));
				String str = br.readLine();
				while (str!=null){
					if (str.indexOf("<param name=\"SRC\" value=\"")!=-1) {
						System.out.println(prefix+str.substring(str.indexOf("<param name=\"SRC\" value=\"")+"<param name=\"SRC\" value=\"".length(), str.lastIndexOf("\""))+"|");
						break;
					}
					str = br.readLine();
				}
				br.close();
			} catch (Exception e) {
				System.out.println(file.getName());
				e.printStackTrace();
			}
			
		}
	}
}
